﻿using Config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using UI.TestModule;
using System.IO;
using Communication.Model.speedData;
using Communication.Model.netPacket;
using Communication.Model.packetType;
using Communication.Model.displacement;
using Communication.Model.acceleration;
using Communication.Model.configuration;
using Communication.Model.slowSignal;
using Communication.TcpIpService;
using Communication.Model.speedRealTVol;
using Communication.Interface;
using UI.Models;
using System.Threading;

namespace UI
{
<<<<<<< HEAD
    public partial class MainForm :Form
    {  
        //收到触发的事件
        private void onDataReceived(NetPacket netPacket)
        {
            //cm.GetDisplacement += ReceivedDispalcement;
            //cm.GetAccData += ReceivedAccData;
            //cm.GetConfig += ReceivedConfig;
            //cm.GetSpeed += ReceivedSpeedData;
            //cm.GetSpeedVol += ReceivedSpeedVol;
            //cm.GetString += ReceivedString;
            //cm.GetSlowSignal += ReceivedSlowSignal;
            //cm.GetMachineTime += ReceiveMachineTime;
            try
            {
                //存储最新的转速包，给DisplacementModel中的字段赋值
                SpeedData rpmsPacket = new SpeedData();
                DateTime time = DateTime.Now;

                switch (netPacket.NetPacketHead.PacketType)
                {
                    case PacketType.DisplacementPacket:
                        #region 接收到位移数据的处理流程
                        //通讯结构体
                        DisplacementData dpt = (DisplacementData)netPacket.DataPacket;
                        ReceivedDispalcement(dpt);

                        #endregion
                        break;
                    case PacketType.AccelerationPacket:
                        //通讯结构体
                        AccData acc = (AccData)netPacket.DataPacket;
                        ReceivedAccData(acc);
                        accDataNow = acc;
                        break;
                    case PacketType.SpeedPacket:
                        //通讯结构体
                        rpmsPacket = (SpeedData)netPacket.DataPacket;
                        ReceivedSpeedData(rpmsPacket);

                        break;
                }
            }
            catch (Exception ex)
            {

                throw;
            }


=======
    //接收到不同类型数据包处理逻辑函数
    public partial class MainForm : Form
    {
        private void ReceivedString(string msg)
        {
            //更新总貌图
            SetControlText(label39, msg);
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
        }
        private void ReceivedDispalcement(DisplacementData dpt)
        {
            //存储达到全局静态变量中
            GlobalVariable.DptCurrent = dpt;
            this.Invoke((EventHandler)delegate
            {
<<<<<<< HEAD
                if (save)
                {
                    string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    string file = "/" +time+ "位移数据" + ".txt";
                    string filepath = @foldPath+ file;
                    FileStream fs = new FileStream(filepath, FileMode.Create);
                    StreamWriter wr = new StreamWriter(fs);
                    for (int i = 0; i < dpt.CHDisData.Length; i++)
                    {
                        wr.WriteLine("通道" + (i + 1) + "：PK2PK：" + dpt.CHDisData[i].PK2PK);
                    }
                    wr.Close();
                    fs.Close();
                    
                    
                }
                //更新总貌图位移 
                if (tabControl_main.SelectedIndex == 0)
=======
                //更新总貌图位移 
                if (tabControl_main.SelectedIndex == 0 || tabControl_main.SelectedIndex == 2)
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                {
                    for (int i = 0; i < 32; i++)
                    {
                        int type = settings.ChannelConfig.Omaplchannels.Channels[i].SensorType;
                        //位移通道
<<<<<<< HEAD
                        //int alarmFlag = dpt.CHDisData[i].AlarmFlag;//获得报警位
=======
                        int alarmFlag = dpt.CHDisData[i].AlarmFlag;//获得报警位
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                        if (type == 0)
                        {
                            Label label = tableLayoutPanel1.Controls[i] as Label;
                            string text = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName + "\r\n\r\n";
                            text += "峰峰值: " + dpt.CHDisData[i].PK2PK.ToString("0.0000") + "um\r\n";
                            text += "间隙电压: " + dpt.CHDisData[i].GapVoltage.ToString("0.0000") + "mA\r\n";
                            //name = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName;
<<<<<<< HEAD
                            int color = 0;
                            //通频阈值报警颜色判断
                            if (enablesetting[i, 4] != 0)
                            {

                                if (dpt.CHDisData[i].RoomSquareValue <= alarmvalue[i, 8])
                                {
                                    color = 1;
                                }
                                if (dpt.CHDisData[i].RoomSquareValue > alarmvalue[i, 8] && dpt.CHDisData[i].RoomSquareValue < alarmvalue[i, 9])

                                { color = 2; }
                                if (dpt.CHDisData[i].RoomSquareValue >= alarmvalue[i, 9])
                                {
                                    color = 3;
                                }
                            }
                            //1倍频报警颜色判断
                            if (enablesetting[i, 5] != 0)
                            {

                                if (dpt.CHDisData[i].OneMultiplierAmp <= alarmvalue[i, 10])
                                {
                                    color = 1;
                                }
                                if (dpt.CHDisData[i].OneMultiplierAmp > alarmvalue[i, 10] && dpt.CHDisData[i].OneMultiplierAmp < alarmvalue[i, 11])

                                { color = 2; }
                                if (dpt.CHDisData[i].OneMultiplierAmp >= alarmvalue[i, 11])
                                {
                                    color = 3;
                                }
                            }
                            //2倍频报警颜色判断
                            if (enablesetting[i, 6] != 0)
                            {

                                if (dpt.CHDisData[i].TWOMultiplierAmp <= alarmvalue[i, 12])
                                {
                                    color = 1;
                                }
                                if (dpt.CHDisData[i].TWOMultiplierAmp > alarmvalue[i, 12] && dpt.CHDisData[i].TWOMultiplierAmp < alarmvalue[i, 13])

                                { color = 2; }
                                if (dpt.CHDisData[i].TWOMultiplierAmp >= alarmvalue[i, 13])
                                {
                                    color = 3;
                                }
                            }
                            //0.5倍频报警颜色判断
                            if (enablesetting[i, 7] != 0)
                            {

                                if (dpt.CHDisData[i].HalfMultiplierAmp <= alarmvalue[i, 14])
                                {
                                    color = 1;
                                }
                                if (dpt.CHDisData[i].HalfMultiplierAmp > alarmvalue[i, 14] && dpt.CHDisData[i].HalfMultiplierAmp < alarmvalue[i, 15])

                                { color = 2; }
                                if (dpt.CHDisData[i].HalfMultiplierAmp >= alarmvalue[i, 15])
                                {
                                    color = 3;
                                }
                            }


                            SetLabelContent(label, text, color);

                        }
=======
                            float RoomSquareValue = dpt.CHDisData[i].RoomSquareValue;
                            float OneMultiplierAmp = dpt.CHDisData[i].OneMultiplierAmp;
                            float TWOMultiplierAmp = dpt.CHDisData[i].TWOMultiplierAmp;
                            float HalfMultiplierAmp = dpt.CHDisData[i].HalfMultiplierAmp;
                            alarmFlag = 0;
                            SetLabelContentDis(label, text, alarmFlag,i, RoomSquareValue, OneMultiplierAmp, TWOMultiplierAmp, HalfMultiplierAmp);
                            //更新位移通道数据列表
                            dataGridView1.Rows[i].Cells["p2p"].Value = dpt.CHDisData[i].PK2PK.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["gapvoltage"].Value = dpt.CHDisData[i].GapVoltage.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["rms"].Value = dpt.CHDisData[i].RoomSquareValue.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["p2p"].Value = dpt.CHDisData[i].PK2PK.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["1xA"].Value = dpt.CHDisData[i].OneMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["1xP"].Value = dpt.CHDisData[i].OneMultiplierPhase.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["2xA"].Value = dpt.CHDisData[i].TWOMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["2xP"].Value = dpt.CHDisData[i].TWOMultiplierPhase.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["halfxA"].Value = dpt.CHDisData[i].HalfMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["halfxP"].Value = dpt.CHDisData[i].HaltMultiplierPhase.ToString("0.0000");

                        }

>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                    }
                }
                //更新图
                else if (tabControl_main.SelectedIndex == 1)
                {
                    int select = comboBox1.SelectedIndex;
                    int type = settings.ChannelConfig.Omaplchannels.Channels[select].SensorType;
                    if (type == 0)
                    {
                        //两个ChartAera设置可见
                        chart1.ChartAreas[0].Visible = true;
                        chart1.ChartAreas[0].Axes[0].Minimum = 0;
                        chart1.ChartAreas[0].Axes[0].Maximum = 1023;
                        chart1.ChartAreas[0].Axes[0].Interval = 100;
                        //chart1.ChartAreas[0].Axes[0].MajorGrid.Interval = System.Double.NaN;
                        //chart1.ChartAreas[0].Axes[1].MajorGrid.Interval = System.Double.NaN;


                        int index = comboBox1.SelectedIndex;
                        float[] time = dpt.CHDisData[index].ZDs;
                        float[] amps = dpt.CHDisData[index].Amps;
                        chart1.Series[0].Color = Color.Red;
                        chart1.Series[0].Points.DataBindY(time);

                        chart1.Series[1].Color = Color.Red;
                        int num = (int)dpt_point_num / 2;
                        float[] axisX = new float[num];
                        for (int i = 0; i < num; i++)
                        {
                            axisX[i] = i;
                        }

                        int rpm_index = settings.ChannelConfig.Omaplchannels.Channels[select].SpeedNo;
                        //根据转速求横坐标
                        if (speed_now.ChSpeeds != null)
                        {
                            string text = "转速" + rpm_index.ToString() + ": " + speed_now.ChSpeeds[rpm_index - 1].rpm.ToString("0.0");
                            float rpm = speed_now.ChSpeeds[rpm_index - 1].rpm;
                            SetControlText(label41, text);
                            if (rpm > 5)
                            {
                                double fs = rpm / 60.0 * 64;
                                double df = fs / dpt_point_num;
                                for (int i = 0; i < num; i++)
                                {
                                    axisX[i] = Convert.ToSingle(i * df);
                                }
                            }
                        }
                        //设置下方图表样式
                        chart1.ChartAreas[1].Visible = true;
                        chart1.ChartAreas[1].Axes[0].IsLogarithmic = false;
                        chart1.ChartAreas[1].Axes[0].Minimum = 1;
                        chart1.ChartAreas[1].Axes[0].Maximum = axisX[num - 1];
                        chart1.ChartAreas[1].Axes[0].MajorGrid.Enabled = true;
                        chart1.ChartAreas[1].Axes[0].MajorGrid.Interval = 100;
                        chart1.ChartAreas[1].Axes[0].MinorGrid.Enabled = false;
                        chart1.ChartAreas[1].Axes[0].MinorTickMark.Enabled = false;                        //绑定数据

                        chart1.Series[1].Points.DataBindXY(axisX, amps);
                        //求区间内的最大值点
                        DataPoint dp = getMaxPoint(chart1.Series[1].Points, chart1.ChartAreas[1].CursorX.SelectionStart, chart1.ChartAreas[1].CursorX.SelectionEnd);
                        if (dp != null)
                        {
                            //画出最大值点
                            //chart1.Series[2].Points.Clear();
                            chart1.Series[2].Points.DataBindXY(new double[] { dp.XValue }, new double[] { dp.YValues[0] });
                            //显示游标值
                            SetControlText(label67, string.Format("{0:F2}Hz-{1:F2}Hz最大值",
                            chart1.ChartAreas[1].CursorX.SelectionStart, chart1.ChartAreas[1].CursorX.SelectionEnd));
                            //显示区间内最大值
                            SetControlText(textBox2, string.Format("{0:F2}um@{1:F2}Hz", dp.YValues[0], dp.XValue));
                        }
                    }
                }
<<<<<<< HEAD
                else if (tabControl_main.SelectedIndex == 2)
                {
                    for (int i = 0; i < 32; i++)
                    {
                        int type = settings.ChannelConfig.Omaplchannels.Channels[i].SensorType;
                        //位移通道
                        //int alarmFlag = dpt.CHDisData[i].AlarmFlag;//获得报警位
                        if (type == 0)
                        {
                            dataGridView1.Rows[i].Cells["p2p"].Value = dpt.CHDisData[i].PK2PK.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["gapvoltage"].Value = dpt.CHDisData[i].GapVoltage.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["rms"].Value = dpt.CHDisData[i].RoomSquareValue.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["1xA"].Value = dpt.CHDisData[i].OneMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["1xP"].Value = dpt.CHDisData[i].OneMultiplierPhase.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["2xA"].Value = dpt.CHDisData[i].TWOMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["2xP"].Value = dpt.CHDisData[i].TWOMultiplierPhase.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["halfxA"].Value = dpt.CHDisData[i].HalfMultiplierAmp.ToString("0.0000");
                            dataGridView1.Rows[i].Cells["halfxP"].Value = dpt.CHDisData[i].HaltMultiplierPhase.ToString("0.0000");
                        }
                    }
                }
=======
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                //更新轴心轨迹
                else if (tabControl_main.SelectedIndex == 4)
                {
                    ////测点ID
                    //testpoint = cb_orbitID.SelectedIndex;
                    ////显示轴心轨迹合成曲线
                    //chart_orbit.Series[0].Points.DataBindXY
                    //(dpt.OrbitData[testpoint].xCoordinate, dpt.OrbitData[testpoint].yCoordinate);
                    ////轴心轨迹信号
                    //chart_orbit_x.Series[0].Points.DataBindY(dpt.OrbitData[testpoint].xCoordinate);
                    //chart_orbit_y.Series[0].Points.DataBindY(dpt.OrbitData[testpoint].yCoordinate);

                    ////显示轴心位置
                    //SetControlText(label_startx, "startX= " + dpt.OrbitData[testpoint].startX.ToString("F2"));
                    //SetControlText(label_starty, "startY= " + dpt.OrbitData[testpoint].startY.ToString("F2"));
                    //SetControlText(label_endx, "CurrentX= " + dpt.OrbitData[testpoint].currentX.ToString("F2"));
                    //SetControlText(label_endy, "CurrentY= " + dpt.OrbitData[testpoint].currentY.ToString("F2"));
                }
                else { }
            });

        }
<<<<<<< HEAD

=======
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
        private void ReceivedAccData(AccData acc)
        {
            try
            {
                //存储到全局静态变量中
                GlobalVariable.AccCurrent = acc;
                this.Invoke((EventHandler)delegate
                {
<<<<<<< HEAD
                    if (save)
                    {
                        string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        string file = "/" + time +"加速度数据" + ".txt";
                        string filepath = @foldPath + file;
                        FileStream fs = new FileStream(filepath, FileMode.Create);
                        StreamWriter wr = new StreamWriter(fs);
                        for (int i = 0; i < acc.CHAccData.Length; i++)
                        {
                            wr.WriteLine("通道" + (i + 1) + "：TotalDB：" + acc.CHAccData[i].TotalDB);
                        }
                        wr.Close();
                        fs.Close();


                    }
                    //更新总貌图加速度          
                    if (tabControl_main.SelectedIndex == 0)//选中是总貌图
=======
                    //更新总貌图加速度          
                    if (tabControl_main.SelectedIndex == 0 || tabControl_main.SelectedIndex == 2)//选中是总貌图
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                    {
                        for (int i = 0; i < 32; i++)
                        {
                            int type = settings.ChannelConfig.Omaplchannels.Channels[i].SensorType;
<<<<<<< HEAD

=======
                            int alarmFlag = acc.CHAccData[i].AlarmFlag;
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                            //加速度通道
                            if (type == 2)
                            {
                                Label label = tableLayoutPanel1.Controls[i] as Label;
                                string text = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName + "\r\n\r\n";
<<<<<<< HEAD
                                text += "振动总值: " + acc.CHAccData[i].TotalDB.ToString("0.0000") + "dB\r\n";
                                text += "平均电压: " + acc.CHAccData[i].AverageVol.ToString("0.0000") + "V\r\n";
                                //name = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName;
                                // MessageBox.Show(i.ToString("0.0000"));调试
                                int color = 0;
                                //高频段报警颜色判断
                                if (enablesetting[i, 0] != 0)
                                {

                                    if (acc.CHAccData[i].highDB <= alarmvalue[i, 0])
                                    {
                                        color = 1;
                                    }
                                    if (acc.CHAccData[i].highDB > alarmvalue[i, 0] && acc.CHAccData[i].highDB < alarmvalue[i, 1])

                                    { color = 2; }
                                    if (acc.CHAccData[i].highDB >= alarmvalue[i, 1])
                                    {
                                        color = 3;
                                    }
                                }
                                //低频段报警颜色判断
                                if (enablesetting[i, 1] != 0)
                                {

                                    if (acc.CHAccData[i].lowDB <= alarmvalue[i, 2])
                                    {
                                        color = 1;
                                    }
                                    if (acc.CHAccData[i].lowDB > alarmvalue[i, 2] && acc.CHAccData[i].lowDB < alarmvalue[i, 3])

                                    { color = 2; }
                                    if (acc.CHAccData[i].lowDB >= alarmvalue[i, 3])
                                    {
                                        color = 3;
                                    }
                                }
                                //加速度总值报警颜色判断
                                if (enablesetting[i, 2] != 0)
                                {

                                    if (acc.CHAccData[i].TotalDB <= alarmvalue[i, 4])
                                    {
                                        color = 1;
                                    }
                                    if (acc.CHAccData[i].TotalDB > alarmvalue[i, 4] && acc.CHAccData[i].TotalDB < alarmvalue[i, 5])

                                    { color = 2; }
                                    if (acc.CHAccData[i].TotalDB >= alarmvalue[i, 5])
                                    {
                                        color = 3;
                                    }
                                }
                                //标志位报警颜色判断
                                if (enablesetting[i, 3] != 0)
                                {

                                    if (acc.CHAccData[i].AlarmFlag == 0)
                                    {
                                        color = 1;
                                    }
                                    if (acc.CHAccData[i].AlarmFlag == 1)

                                    { color = 2; }
                                    if (acc.CHAccData[i].AlarmFlag == 2)
                                    {
                                        color = 3;
                                    }
                                }

                                SetLabelContent(label, text, color);
=======
                                text += "振动总值recieve: " + acc.CHAccData[i].TotalDB.ToString("0.0000") + "dB\r\n";
                                text += "平均电压: " + acc.CHAccData[i].AverageVol.ToString("0.0000") + "V\r\n";
                                //name = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName;
                                float TotalDB = acc.CHAccData[i].TotalDB;
                                float highDB = acc.CHAccData[i].highDB;
                                float lowDB = acc.CHAccData[i].lowDB;
                                //报警+更新数据
                                SetLabelContentAcc(label, text, alarmFlag,i, TotalDB, highDB,lowDB,i,acc);
                              
                                
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                            }
                        }
                    }
                    //更新图
                    else if (tabControl_main.SelectedIndex == 1)
                    {
                        int select = comboBox1.SelectedIndex;
                        int type = settings.ChannelConfig.Omaplchannels.Channels[select].SensorType;
                        //两个ChartAera设置可见
                        if (type == 2)
                        {
                            chart1.ChartAreas[0].Visible = false;
                            chart1.ChartAreas[1].Visible = true;
                            //chart1.ChartAreas[1].Axes[0].IsLogarithmic = true;
                            float[] valueY = acc.CHAccData[select].FFTAmpDB;
                            switch (axisYUnit)
                            {
                                case AxisYUnit.ms2:
                                    valueY = acc.CHAccData[select].FFTAmp;
                                    break;
                                default:
                                    break;
                            }
                            float[] x = new float[65536];
                            for (int i = 0; i < x.Length; i++)
                            {
                                x[i] = (i) * 25600.0f / 131072.0f;
                                //data[i] = Convert.ToSingle(Math.Log10(data[i]) * 20 + 120.0f);
                            }
                            chart1.ChartAreas[1].Axes[0].IsLogarithmic = true;
                            chart1.ChartAreas[1].Axes[0].Minimum = 1;
                            chart1.ChartAreas[1].Axes[0].Maximum = 12800;
                            chart1.ChartAreas[1].Axes[0].Interval = System.Double.NaN;
                            //chart1.ChartAreas[1].Axes[0].Interval = 10;
                            chart1.ChartAreas[1].Axes[0].MajorGrid.Enabled = true;
                            chart1.ChartAreas[1].Axes[0].MajorGrid.Interval = 1;
                            chart1.ChartAreas[1].Axes[0].MinorGrid.Enabled = true;
                            chart1.ChartAreas[1].Axes[0].MinorGrid.Interval = 1;
                            //chart1.ChartAreas[1].Axes[0].MajorGrid.Enabled = true;
                            //chart1.ChartAreas[1].Axes[0].MajorGrid.Interval = 0.1;
                            chart1.Series[1].Color = Color.Red;
                            chart1.Series[1].Points.DataBindXY(x, valueY);

                            int rpm_index = settings.ChannelConfig.Omaplchannels.Channels[select].SpeedNo;
                            if (speed_now.ChSpeeds != null)
                            {
                                string text = "转速" + rpm_index.ToString() + ": " + speed_now.ChSpeeds[rpm_index - 1].rpm.ToString("0.0");
                                SetControlText(label41, text);
                            }
                            DataPoint dp = getMaxPoint(chart1.Series[1].Points, chart1.ChartAreas[1].CursorX.SelectionStart, chart1.ChartAreas[1].CursorX.SelectionEnd);
                            if (dp != null)
                            {
                                //画出最大值点
                                //chart1.Series[2].Points.Clear();
                                chart1.Series[2].Points.DataBindXY(new double[] { dp.XValue }, new double[] { dp.YValues[0] });
                                //显示游标值
                                SetControlText(label67, string.Format("{0:F2}Hz-{1:F2}Hz最大值",
                                    chart1.ChartAreas[1].CursorX.SelectionStart, chart1.ChartAreas[1].CursorX.SelectionEnd));
                                //显示区间内最大值
                                SetControlText(textBox2, string.Format("{0:F2}@{1:F2}Hz", dp.YValues[0], dp.XValue));
                            }
                        }

                    }
                    //更新数据列表
                    else if (tabControl_main.SelectedIndex == 2 && tabControl3.SelectedIndex == 1)
                    {
<<<<<<< HEAD
                        for (int i = 0; i < 32; i++)
                        {
                            int type = settings.ChannelConfig.Omaplchannels.Channels[i].SensorType;

                            //加速度通道
                            if (type == 2)
                            {
                                dataGridView2.Rows[i - DisplacementNo].Cells["TotalDB"].Value = acc.CHAccData[i].TotalDB.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["highDB"].Value = acc.CHAccData[i].highDB.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["lowDB"].Value = acc.CHAccData[i].lowDB.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["velocityRMS"].Value = acc.CHAccData[i].velocityRMS.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["AverageVol"].Value = acc.CHAccData[i].AverageVol.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["displacementRMS"].Value = acc.CHAccData[i].displacementRMS.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["1xA"].Value = acc.CHAccData[i].OneMultiplierAmp.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["2xA"].Value = acc.CHAccData[i].TWOMultiplierAmp.ToString("0.0000");
                                dataGridView2.Rows[i - DisplacementNo].Cells["3xA"].Value = acc.CHAccData[i].HalfMultiplierAmp.ToString("0.0000");
                                //报警+更新数据
                            }
                        }
=======
                        DataTable dt = new DataTable();
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                    }
                    else if (tabControl_main.SelectedIndex == 5)
                    {
                        float[] octave = Helper.OctaveHelper.CalculateOctave_1_3();
                        string[] octaves = octave.Select(p => Math.Round(p).ToString("f0")).ToArray();
                        int id = int.Parse(cb_octave.SelectedItem.ToString().Split(':')[0]);
                        float[] values = acc.CHAccData[id]._1_3otcaveDB.Select(p => (float)Math.Round(p)).ToArray();
                        //柱状图
                        //chart_octave.Series[0].Points.DataBindXY(octave, acc.CHAccData[id]._1_3otcaveDB);
                        chart_octave.Series[0].Points.DataBindXY(octaves, acc.CHAccData[id]._1_3otcaveDB);
                        //chart_octave.Series[0].Points.DataBindY(acc.CHAccData[id]._1_3otcaveDB);
                        //绑定
                        for (int i = 0; i < 31; i++)
                        {
                            datagrid_octave.Rows.Add();
                            datagrid_octave.Rows[i].Cells["Freq"].Value = octaves[i];
                            datagrid_octave.Rows[i].Cells["Amp"].Value = values[i].ToString();
                        }
                    }
<<<<<<< HEAD
           
=======

>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
<<<<<<< HEAD

        private void ReceivedString(string msg)
        {
            //更新总貌图
            SetControlText(label39, msg);
        }


=======
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
        private void ReceivedConfig(ACQParametersConfig config)
        {

        }
        public void ReceivedSpeedData(SpeedData speed)
        {
<<<<<<< HEAD
            _ = this.Invoke((EventHandler)delegate
              {
                  if (save)
                  {
                      string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                      string file = "/" + time+ "转速数据" + ".txt";
                      string filepath = @foldPath + file;
                      FileStream fs = new FileStream(filepath, FileMode.Create);
                      StreamWriter wr = new StreamWriter(fs);
                      for (int i = 0; i < speed.ChSpeeds.Length; i++)
                      {
                          wr.WriteLine("通道" + (i + 1) + "：转速：" + speed.ChSpeeds[i].rpm);
                      }
                      wr.Close();
                      fs.Close();


                  }
                  //MessageBox.Show("收到转速数据包");
                  //存储实时转速
                  speed_now = speed;
                  string content = DateTime.Now.ToString() + ",";
                  //更新总貌图转速
                  for (int i = 0; i < 6; i++)
                  {
                      Control ctrl = tableLayoutPanel1.Controls[i + 32];
                      string text = settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].ChannelName + "\r\n\r\n";
                      text += "转速" + (i + 1).ToString() + ": " + speed.ChSpeeds[i].rpm.ToString("0.0") + "\r\n";
                      //name = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName;
                      SetControlText(ctrl, text);
                      content += speed.ChSpeeds[i].rpm.ToString("0.00");
                      content += ",";
                  }
              });
=======
            this.Invoke((EventHandler)delegate
            {
                //存储实时转速
                speed_now = speed;
                string content = DateTime.Now.ToString() + ",";
                //更新总貌图转速
                for (int i = 0; i < 6; i++)
                {
                    Control ctrl = tableLayoutPanel1.Controls[i + 32];
                    string text = settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].ChannelName + "\r\n\r\n";
                    text += "转速" + (i + 1).ToString() + ": " + speed.ChSpeeds[i].rpm.ToString("0.0") + "\r\n";
                    //name = settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName;
                    SetControlText(ctrl, text);
                    content += speed.ChSpeeds[i].rpm.ToString("0.00");
                    content += ",";
                }
                if (FlagSave != 0)
                {
                    if (PathSave != "")
                    {
                        TxtOperate.AppendLine(PathSave, content);
                    }
                }
                if (tabControl_main.SelectedIndex == 6)//如果选择转速时域波形图
                {
                    int select = comboBox2.SelectedIndex;//获得1-6哪个通道



                    chart_rpm.ChartAreas[0].Visible = false;
                    chart_rpm.ChartAreas[1].Visible = true;
                    //chart1.ChartAreas[1].Axes[0].IsLogarithmic = true;
                    float[] valueY = speed.ChSpeeds[select].RpmTimewave; //获得该通道 y轴坐标
                    switch (axisYUnit)
                    {
                        case AxisYUnit.ms2:
                            valueY = speed.ChSpeeds[select].RpmTimewave;
                            break;
                        default:
                            break;
                    }
                    float[] x = new float[65536];
                    for (int i = 0; i < x.Length; i++)
                    {
                        x[i] = (i) * 25600.0f / 131072.0f;
                        //data[i] = Convert.ToSingle(Math.Log10(data[i]) * 20 + 120.0f);
                    }
                    chart_rpm.ChartAreas[1].Axes[0].IsLogarithmic = true;
                    chart_rpm.ChartAreas[1].Axes[0].Minimum = 1;
                    chart_rpm.ChartAreas[1].Axes[0].Maximum = 12800;
                    chart_rpm.ChartAreas[1].Axes[0].Interval = System.Double.NaN;
                    //chart1.ChartAreas[1].Axes[0].Interval = 10;
                    chart_rpm.ChartAreas[1].Axes[0].MajorGrid.Enabled = true;
                    chart_rpm.ChartAreas[1].Axes[0].MajorGrid.Interval = 1;
                    chart_rpm.ChartAreas[1].Axes[0].MinorGrid.Enabled = true;
                    chart_rpm.ChartAreas[1].Axes[0].MinorGrid.Interval = 1;
                    //chart1.ChartAreas[1].Axes[0].MajorGrid.Enabled = true;
                    //chart1.ChartAreas[1].Axes[0].MajorGrid.Interval = 0.1;
                    chart_rpm.Series[1].Color = Color.Red;
                    chart_rpm.Series[1].Points.DataBindXY(x, valueY);


                }
            });
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
        }
        public void ReceivedSpeedVol(SpeedRealTVol data)
        {
            this.Invoke((EventHandler)delegate
            {
                if (tabControl2.SelectedIndex == 2 && tabControl_main.SelectedIndex == 3)
                {
                    int id = listBox2.SelectedIndex;
                    SetControlText(tb_key_max, data.ChVols[id].TopVolt.ToString());
                    SetControlText(tb_key_min, data.ChVols[id].BotmVolt.ToString());
                    SetControlText(tb_gear_max, data.ChVols[id].TopVolt.ToString());
                    SetControlText(tb_gear_min, data.ChVols[id].BotmVolt.ToString());
                }
            });
        }
        private void ReceiveMachineTime(MachineTime machineTime)
        {
            this.Invoke((EventHandler)delegate
            {
                int Year = Convert.ToInt32(machineTime.Year);
                int Month = Convert.ToInt32(machineTime.Month);
                int Day = Convert.ToInt32(machineTime.Day);
                int Hour = Convert.ToInt32(machineTime.Hour);
                int Minute = Convert.ToInt32(machineTime.Minute);
                int Second = Convert.ToInt32(machineTime.Second);
                DateTime time = new DateTime(Year, Month, Day, Hour, Minute, Second);

                Status_time.Text = time.ToString();
            });
        }
        private void ReceivedSlowSignal(SlowSignal ss)
        {

        }
<<<<<<< HEAD
        //设置总览图文字，颜色
        private void SetLabelContent(Label label, string name, int color)
        {
            if (label.IsHandleCreated && label.InvokeRequired)
            {
                label.Invoke(new Action<Label, string, int>(SetLabelContent), label, name, color);
                return;
            }
            switch (color)
            {
                case 0: label.BackColor = Color.White; break;

                case 1: label.BackColor = Color.Green; break;

                case 2: label.BackColor = Color.Yellow; break;

                case 3: label.BackColor = Color.Red; break;
            }
            label.Text = name;

        }

        private void SetComboIndex(ComboBox ctrl, int index)
        {
            if (ctrl.IsHandleCreated && ctrl.InvokeRequired)
            {
                ctrl.Invoke(new Action<ComboBox, int>(SetComboIndex), ctrl, index);
                return;
            }
            if (index >= 0)
                ctrl.SelectedIndex = index;
        }
=======
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
    }
}
